import { computed, unref } from 'vue'

export function useFieldBinding(formData, config, options) {
    const rowIndex = options.rowIndex
    const parentId = options.parentId

    const fieldValue = computed({
        get() {
            if (parentId && typeof rowIndex === 'number') {
                return unref(formData)?.[parentId]?.[rowIndex]?.[config.id]
            }
            return unref(formData)?.[config.id]
        },
        set(val) {
            if (parentId && typeof rowIndex === 'number') {
                formData.value[parentId][rowIndex][config.id] = val
            } else {
                formData.value[config.id] = val
            }
        }
    })

    return {
        fieldValue
    }
}